﻿using System;
using System.Collections.Generic;

using System.Text;

namespace RoughJs.Ast
{
    /// <summary>
    /// 2010-01-5 ----- 2010-01-29
    /// 陈曦 1.0
    /// Microsoft Public License (Ms-PL)
    ///This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the license, do not use the software.
    ///1. Definitions
    ///The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under U.S. copyright law.
    ///A "contribution" is the original software, or any additions or changes to the software.
    ///A "contributor" is any person that distributes its contribution under this license.
    ///"Licensed patents" are a contributor's patent claims that read directly on its contribution.
    ///2. Grant of Rights
    ///(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
    ///(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.
    ///3. Conditions and Limitations
    ///(A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
    ///(B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.
    ///(C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.
    ///(D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.
    ///(E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement. 
    /// </summary>
    public class InterpreterData : DebuggableScript
    {
        static int INITIAL_MAX_ICODE_LENGTH = 1024;
        static int INITIAL_STRINGTABLE_SIZE = 64;
        static int INITIAL_NUMBERTABLE_SIZE = 64;

        public InterpreterData(int languageVersion,
                        String sourceFile, String encodedSource)
        {
            this.languageVersion = languageVersion;
            this.itsSourceFile = sourceFile;
            this.encodedSource = encodedSource;

            init();
        }

        public InterpreterData(InterpreterData parent)
        {
            this.parentData = parent;
            this.languageVersion = parent.languageVersion;
            this.itsSourceFile = parent.itsSourceFile;
            this.encodedSource = parent.encodedSource;

            init();
        }

        private void init()
        {
            itsICode = new sbyte[INITIAL_MAX_ICODE_LENGTH];
            itsStringTable = new String[INITIAL_STRINGTABLE_SIZE];
        }

        public String itsName;
        public String itsSourceFile;
        public bool itsNeedsActivation;
        public int itsFunctionType;

        public String[] itsStringTable;
        public double[] itsDoubleTable;
        public InterpreterData[] itsNestedFunctions;
        public Object[] itsRegExpLiterals;

        public sbyte[] itsICode;

        public int[] itsExceptionTable;

        public int itsMaxVars;
        public int itsMaxLocals;
        public int itsMaxStack;
        public int itsMaxFrameArray;

        // see comments in NativeFuncion for definition of argNames and argCount
        public String[] argNames;
        public bool[] argIsConst;
        public int argCount;

        public int itsMaxCalleeArgs;

        public String encodedSource;
        public int encodedSourceStart;
        public int encodedSourceEnd;

        public int languageVersion;

        public bool useDynamicScope;

        public bool topLevel;

        public Object[] literalIds;

        public UintMap longJumps;

        public int firstLinePC = -1; // PC for the first LINE icode

        public InterpreterData parentData;

        public bool evalScriptFlag; // true if script corresponds to eval() code

        public bool isTopLevel()
        {
            return topLevel;
        }

        public bool isFunction()
        {
            return itsFunctionType != 0;
        }

        public String getFunctionName()
        {
            return itsName;
        }

        public int getParamCount()
        {
            return argCount;
        }

        public int getParamAndVarCount()
        {
            return argNames.Length;
        }

        public String getParamOrVarName(int index)
        {
            return argNames[index];
        }

        public bool getParamOrVarConst(int index)
        {
            return argIsConst[index];
        }

        public String getSourceName()
        {
            return itsSourceFile;
        }

        public bool isGeneratedScript()
        {
            return ScriptRuntime.isGeneratedScript(itsSourceFile);
        }

        public int[] getLineNumbers()
        {
            return Interpreter.getLineNumbers(this);
        }

        public int getFunctionCount()
        {
            return (itsNestedFunctions == null) ? 0 : itsNestedFunctions.Length;
        }

        public DebuggableScript getFunction(int index)
        {
            return itsNestedFunctions[index];
        }

        public DebuggableScript getParent()
        {
            return parentData;
        }
    }
}
